<template>
  <div class="base-pagination">
    <div class="pagination-controls">
      <el-button
        :disabled="currentPage === 1"
        @click="goFirstPage"
      >
        <el-icon><DArrowLeft /></el-icon>
      </el-button>
      <el-button
        :disabled="currentPage === 1"
        @click="goPrevPage"
      >
        <el-icon><ArrowLeft /></el-icon>
      </el-button>
      <span class="page-info">{{ currentPage }} / {{ totalPages }}</span>
      <el-button
        :disabled="currentPage === totalPages"
        @click="goNextPage"
      >
        <el-icon><ArrowRight /></el-icon>
      </el-button>
      <el-button
        :disabled="currentPage === totalPages"
        @click="goLastPage"
      >
        <el-icon><DArrowRight /></el-icon>
      </el-button>
    </div>
    <div class="jump-page">
      <span>前往</span>
      <el-input
        v-model="jumpPage"
        class="jump-input"
        @keyup.enter="handleJumpPage"
      />
      <span>页</span>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { ArrowLeft, DArrowLeft, ArrowRight, DArrowRight } from '@element-plus/icons-vue'

const props = defineProps({
  // 当前页码
  currentPage: {
    type: Number,
    required: true
  },
  // 总条数
  total: {
    type: Number,
    required: true
  },
  // 每页条数
  pageSize: {
    type: Number,
    default: 10
  }
})

const emit = defineEmits(['update:currentPage'])

// 跳转页码
const jumpPage = ref('')

// 计算总页数
const totalPages = computed(() => Math.ceil(props.total / props.pageSize))

// 处理页码跳转
const handleJumpPage = () => {
  const pageNum = parseInt(jumpPage.value)
  if (pageNum && pageNum > 0 && pageNum <= totalPages.value) {
    emit('update:currentPage', pageNum)
  } else {
    ElMessage.warning('请输入有效的页码')
  }
  jumpPage.value = ''
}

// 跳转到第一页
const goFirstPage = () => {
  if (props.currentPage !== 1) {
    emit('update:currentPage', 1)
  }
}

// 跳转到最后一页
const goLastPage = () => {
  if (props.currentPage !== totalPages.value) {
    emit('update:currentPage', totalPages.value)
  }
}

// 上一页
const goPrevPage = () => {
  if (props.currentPage > 1) {
    emit('update:currentPage', props.currentPage - 1)
  }
}

// 下一页
const goNextPage = () => {
  if (props.currentPage < totalPages.value) {
    emit('update:currentPage', props.currentPage + 1)
  }
}
</script>

<style lang="scss" scoped>
.base-pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 12px 20px;
  background: #fff;
  border-top: 1px solid #ebeef5;
  gap: 16px;
  margin-top: auto;
}

.pagination-controls {
  display: flex;
  align-items: center;
  gap: 8px;

  .el-button {
    padding: 6px;
    height: 28px;
    width: 28px;
    font-size: 13px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .page-info {
    font-size: 14px;
    color: #606266;
    margin: 0 8px;
    min-width: 50px;
    text-align: center;
  }
}

.jump-page {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: #606266;
  
  .jump-input {
    width: 50px;
    
    :deep(.el-input__inner) {
      height: 28px;
      line-height: 28px;
      padding: 0 4px;
      text-align: center;
    }
  }
}

/* 移动端适配 */
@media screen and (max-width: 768px) {
  .base-pagination {
    padding: 8px 16px;
    gap: 8px;
    flex-wrap: wrap;
  }

  .pagination-controls {
    width: 100%;
    justify-content: center;
    gap: 12px;

    .el-button {
      padding: 6px;
      min-width: 28px;
      width: 28px;
    }

    .page-info {
      min-width: 40px;
      margin: 0;
    }
  }

  .jump-page {
    width: 100%;
    justify-content: center;
  }
}
</style> 